<?php
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/6/19 14:48
 * Desc:
 */


class Model_Api_V2_Index
{
    /**
     * 获取热门产品
     * @param $type_id
     * @param $row
     * @return array|mixed
     */
    public static function get_hot_product($type_id, $params = array())
    {
        /**
         * @var $image_w int
         * @var $image_h int
         * @var $row int
         * @var $offset int
         */
        extract($params);

        $offset = isset($offset) ? $offset : 0;
        $image_w = isset($image_w) ? $image_w : 750;
        $image_h = isset($image_h) ? $image_h : 750;
        $platform_no = isset($platform_no) ? $platform_no : 1;

        $pinyin = Model_Model::all_model($type_id, 'pinyin');
        $pinyin = $pinyin == 'ship_line' ? 'ship' : $pinyin;
        $list   = array();
        if (!empty($pinyin)) {
            if ($type_id > 199) {
                $modelInfo = DB::select()
                    ->from("model")
                    ->where('id', '=', $type_id)
                    ->execute()
                    ->current();
                $row = isset($row) && $row ? $row : 10;
                $list      = Model_Api_V2_Index::get_list_order($modelInfo, $offset, $row, $pinyin);
            } elseif ($type_id == 104) {
                $params = array(
                    'limit' => isset($row) && $row ? $row : 10,
                    'offset' => $offset
                );
                $list = Taglib_Ship::common_product($params);
            } else {
                $row = isset($row) && $row ? $row : 10;
                $tag  = Taglib_ . ucfirst($pinyin);
                $list = $tag::query(array('flag' => 'order', 'row' => $row, 'offset' => $offset,'platform'=>$platform_no));
            }

            $uni_img = Model_Uniapp_Images::get_img_list($type_id);
            foreach ($list as &$item) {
                $filed = "litpic,title,satisfyscore,price,id,sellnum,address,product_video,author";
                if ($type_id == 13) {
                    $filed = "litpic,title,satisfyscore,price,id,sellnum,expire,coming,jsendtime,discount";
                }
                if ($type_id == 3) {
                    //租车的使用类型 1租车 0用车
                    $filed .= ",use_type";
                }
                $item           = Model_Api_V2_Common::res_data($filed, $item);
                $item['typeid'] = $type_id;
                if(isset($uni_img[$item['id']])){
                    $item['litpic'] = Model_Api_V2_System::uniapp_img($uni_img[$item['id']], $image_w, $image_h);
                }else{
                    $item['litpic'] = Model_Api_V2_System::uniapp_img($item['litpic'], $image_w, $image_h);
                }
            }
        }
        return $list;
    }

    /**
     * 通过type_id获取移动端可使用的导航
     * @param array $type_ids
     * @return array
     */
    public static function get_m_nav_by_type_ids(array $type_ids)
    {
        return DB::select('m_typeid', 'm_title')
            ->from('m_nav')
            ->where('m_isopen', '=', '1')
            ->and_where('m_typeid', 'in', $type_ids)
            ->order_by('m_displayorder', 'asc')
            ->group_by('m_typeid')
            ->execute()
            ->as_array();
    }

    /**
     * 格式化数据
     * @param $data
     * @param $type_id
     * @return mixed
     */
    public static function format_wenlv_data($data, $type_id) {
        $model_name = Model_Model::get_module_name($type_id);
        foreach ($data as &$val) {
            $val['model_name'] = $model_name;
            $val['has_video']  = isset($val['product_video']) && $val['product_video'];
        }
        return $data;
    }

    /**
     * 获取应用产品列表
     * @param $modelInfo
     * @param $offset
     * @param $row
     * @param $pinyin
     * @return array
     */
    public static function get_list_order($modelInfo, $offset, $row, $pinyin)
    {
        $typeid   = $modelInfo['id'];
        $addtable = $modelInfo['addtable'];
        $addtable = 'sline_' . $addtable;

        $w    = " where a.ishidden=0 and a.typeid={$typeid} ";
        $sql  = "SELECT a.* FROM sline_model_archive as a " .
            " LEFT JOIN {$addtable} b on (a.id=b.productid) " .
            " LEFT JOIN sline_allorderlist c on (a.id=c.aid and c.typeid={$typeid})" .
            $w .
            " ORDER BY ifnull(c.displayorder,9999) asc " .
            " limit {$offset},{$row}";
        $list = DB::query(Database::SELECT, $sql)->execute()->as_array();
        foreach ($list as &$v) {
            $v['price'] = Model_Tongyong::get_minprice($v['id'], array('info' => $v));
            $v['commentnum'] = Model_Comment::get_comment_num($v['id'], $typeid); //评论次数
            $v['sellnum']    = Model_Member_Order::get_sell_num($v['id'], $typeid) + intval($v['bookcount']); //销售数量
            $v['sellprice']  = Model_Tongyong::get_min_sellprice($v['id']);
            $v['attrlist']     = Model_Model_Attr::get_attr_list($v['attrid'], $typeid);//属性列表.
            $v['url']          = Common::get_web_url($v['webid']) . "/" . $pinyin . "/show_{$v['aid']}.html";
            $v['satisfyscore'] = St_Functions::get_satisfy($typeid, $v['id'], $v['satisfyscore'], array('suffix' => ''));
        }
        return $list;
    }

    /**
     * @return array
     * 通过订单销售数量，获取销量前20的产品供前台展示
     */
    public static function get_order_hot_product()
    {
        $list = DB::select(DB::expr("productautoid as id,typeid,SUM( dingnum ) AS total_sales"))
            ->from('member_order')
            ->where('typeid', 'in', [1,2,3,5,8,21,23,104,114,118,121])
            ->group_by(DB::expr("productautoid,typeid"))
            ->order_by('total_sales', 'DESC')
            ->limit(20)
            ->execute()->as_array();

        $product_list = [];
        foreach ($list as $v) {
            $uni_img = Model_Uniapp_Images::get_img_list($v['typeid']);
            $pinyin  = Model_Model::all_model($v['typeid'], 'pinyin');
            $pinyin  = $pinyin == 'ship_line' ? 'ship' : $pinyin;
            if($v['typeid']==5){
                $product = DB::select()->from($pinyin)->where('id', '=', $v['id'])->and_where('ishidden','=',0)->execute()->current();
            }else{
                $product = DB::select()->from($pinyin)->where('id', '=', $v['id'])->and_where('status','=',3)->execute()->current();
            }
            if ($product) {
                $filed = "litpic,title,satisfyscore,price,id,sellnum,address,product_video,author";
                if ($v['typeid'] == 13) {
                    $filed = "litpic,title,satisfyscore,price,id,sellnum,expire,coming,jsendtime,discount";
                }
                if ($v['typeid'] == 3) {
                    $filed .= ",use_type";
                }
                if(isset($uni_img[$product['id']])){
                    $product['litpic'] = Model_Api_V2_System::uniapp_img($uni_img[$product['id']], 750, 750);
                }else{
                    $product['litpic'] = Model_Api_V2_System::uniapp_img($product['litpic'], 750, 750);
                }
                $item = Model_Api_V2_Common::res_data($filed, $product);
                $item['typeid'] = $v['typeid'];

                $product_list[] = $item;
            }
        }

        return $product_list;
    }
}